package net.chrisrichardson.ec2deployer.applications

import net.chrisrichardson.ec2deployer.core.AwsUtilities
import net.chrisrichardson.ec2deployer.core.EC2Server

public class DirectorySnapshot {

    String s3Path
    String directory

    DirectorySnapshot() {

    }
    
    DirectorySnapshot(String directory, String s3Path) {
        this.directory = directory
        this.s3Path = s3Path
    }


    static DirectorySnapshot create(EC2Server host, String directory) {
        String object = UUID.randomUUID().toString() + ".tar"
        String bucket = "${host.awsProperties.accessKey}.tmp-clone-db"
        def tarFile = "/var/tmp/snapshot.tar"
        def s3Path = "$bucket/$object"

        host.ssh "cd $directory; tar cf $tarFile ."

        AwsUtilities awsUtilities = new AwsUtilities(host)
        awsUtilities.s3put s3Path, tarFile

        return new DirectorySnapshot(directory, s3Path)

    }

    void install(EC2Server host) {
        def tarFile = "/var/tmp/snapshot.tar"
        AwsUtilities awsUtilities = new AwsUtilities(host)
        awsUtilities.s3get s3Path, "/var/tmp/snapshot.tar"
        host.ssh "cd $directory; tar xf $tarFile"
    }
}